package com.gengzp.ops.controller;

import com.gengzp.common.constants.PermissionConstants;
import com.gengzp.common.document.annotations.Api;
import com.gengzp.common.document.annotations.ApiOperation;
import com.gengzp.common.model.base.req.SearchByIdReq;
import com.gengzp.common.model.base.resp.Result;
import com.gengzp.common.model.database.req.AddSysDatabaseReq;
import com.gengzp.common.model.database.req.EditSysDatabaseReq;
import com.gengzp.common.model.database.req.PageSysDatabaseReq;
import com.gengzp.common.model.database.resp.PageSysDatabaseResp;
import com.gengzp.common.model.database.resp.SearchSysDatabaseResp;
import com.gengzp.common.model.pagination.resp.PaginationResult;
import com.gengzp.ops.service.ISysDatabaseService;
import com.gengzp.permission.annotations.CheckPermission;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName DataBaseController
 * @Description 数据库管理模块
 * @Author gengzp
 * @Date 2025/7/7 19:25
 */
@RestController
@RequestMapping("/database")
@Api(tags = "数据库管理模块")
public class SysDatabaseController {

    @Autowired
    private ISysDatabaseService databaseService;

    /**
     * 添加数据库连接信息
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加数据库连接信息")
    @CheckPermission(PermissionConstants.DATABASE_WRITE)
    public Result<Void> addSysDatabase(@Valid @RequestBody AddSysDatabaseReq req) {
        databaseService.addSysDatabase(req);
        return Result.getSuccessResult();
    }

    /**
     * 编辑数据库连接信息
     */
    @PostMapping("/edit")
    @ApiOperation(value = "编辑数据库连接信息")
    @CheckPermission(PermissionConstants.DATABASE_WRITE)
    public Result<Void> editSysDatabase(@Valid @RequestBody EditSysDatabaseReq req) {
        databaseService.editSysDatabase(req);
        return Result.getSuccessResult();
    }

    /**
     * 删除数据库连接信息
     */
    @PostMapping("/delete_by_id")
    @ApiOperation(value = "删除数据库连接信息")
    @CheckPermission(PermissionConstants.DATABASE_WRITE)
    public Result<Void> deleteDatabaseById(@Valid @RequestBody SearchByIdReq req) {
        databaseService.deleteSysDatabaseById(Long.valueOf(req.getId()));
        return Result.getSuccessResult();
    }

    /**
     * 分页查询数据库连接信息
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询数据库连接信息")
    public Result<PaginationResult<PageSysDatabaseResp>> pageQuerySysDatabase(@Valid @RequestBody PageSysDatabaseReq req) {
        return Result.getSuccessResult(databaseService.pageQuerySysDatabase(req));
    }

    /**
     * 根据id查询数据库详情
     */
    @PostMapping("/search_by_id")
    @ApiOperation(value = "根据id查询数据库详情")
    @CheckPermission(PermissionConstants.DATABASE_READ)
    public Result<SearchSysDatabaseResp> searchDatabaseById(@Valid @RequestBody SearchByIdReq req) {
        return Result.getSuccessResult(databaseService.searchSysDatabaseById(Long.valueOf(req.getId())));
    }

}
